// SlagCar/OA/OA.js
import {GetUrlString} from "../../utils/util";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: [0,1,2,3,4,5,6,7,8,9],
    navs: [],
    /*[
      {
        title: '挖机管理',
        nav:[
          {name: '供应商挖机台班登记',url: '/DIYPages/general_entity/general_entity?entity=e_gongyingshangwajitaibandengji',icon: ''}
          ,{name: '挖机拖车登记表',url: '/DIYPages/general_entity/general_entity?entity=e_wajituochedengjibiao',icon: ''}
          ,{name: '挖机加油充值申请单',url: '/DIYPages/general_entity/general_entity?entity=e_wajijiayouchongzhishenqingdan',icon: ''}
          ,{name: '其他加油申请表',url: '/DIYPages/general_entity/general_entity?entity=e_qitajiayoushenqingbiao',icon: ''}
        ]
      }
      ,{
        title: '人事工具箱',
        nav:[
          {name: '入职审批',url: '/DIYPages/general_entity/general_entity?entity=e_ruzhishenpi',icon: ''}
          ,{name: '补卡申请',url: '/DIYPages/general_entity/general_entity?entity=e_bukashenqing',icon: ''}
          ,{name: '请假',url: '/DIYPages/general_entity/general_entity?entity=e_qingjia',icon: ''}
          ,{name: '修改个人档案',url: '/DIYPages/general_entity/general_entity?entity=e_xiugaigerendangan',icon: ''}
          ,{name: '人员录用申请',url: '/DIYPages/general_entity/general_entity?entity=e_renyuanluyongshenqing',icon: ''}
          ,{name: '离职申请',url: '/DIYPages/general_entity/general_entity?entity=e_lizhishenqing',icon: ''}

        ]
      }
      ,{
        title: '车辆管理',
        nav:[
          {name: '车辆维修申请',url: '/DIYPages/general_entity/general_entity?entity=e_cheliangweixiushenqing',icon: ''}
          ,{name: '车辆维修废料',url: '/DIYPages/general_entity/general_entity?entity=e_cheliangweixiufeiliao',icon: ''}
          ,{name: '证照使用申请',url: '/DIYPages/general_entity/general_entity?entity=e_zhengzhaoshiyongshenqing',icon: ''}
          ,{name: '加油充值申请单',url: '/DIYPages/general_entity/general_entity?entity=e_jiayouchongzhishenqingdan',icon: ''}
          ,{name: '日常加油登记',url: '/DIYPages/general_entity/general_entity?entity=e_richangjiayoudengji',icon: ''}
          ,{name: '车辆检查记录',url: '/DIYPages/general_entity/general_entity?entity=e_cheliangjianchajilu',icon: ''}
          ,{name: '补胎换新胎',url: '/DIYPages/general_entity/general_entity?entity=e_butaihuanxintai',icon: ''}
          ,{name: '打黄油登记',url: '/DIYPages/general_entity/general_entity?entity=e_dahuangyoudengji',icon: ''}
          ,{name: '领料申请单',url: '/DIYPages/general_entity/general_entity?entity=e_lingsonghuodanshenqing',icon: ''}
          ,{name: '用车',url: '/DIYPages/general_entity/general_entity?entity=e_yongche',icon: ''}
          ,{name: '出车计划',url: '/DIYPages/general_entity/general_entity?entity=e_chuchejihua',icon: ''}

        ]
      }
      ,{
        title: '财务审批',
        nav:[
          {name: '挖机 财务报销',url: '/DIYPages/general_entity/general_entity?entity=e_wajicaiwubaoxiao',icon: ''}
          ,{name: '商旅出差',url: '/DIYPages/general_entity/general_entity?entity=e_shanglvchucha',icon: ''}
          ,{name: '商旅报销',url: '/DIYPages/general_entity/general_entity?entity=e_shanglvbaoxiao',icon: ''}
          ,{name: '个人报销',url: '/DIYPages/general_entity/general_entity?entity=e_gerenbaoxiao',icon: ''}
          ,{name: '借款申请',url: '/DIYPages/general_entity/general_entity?entity=e_jiekuanshenqing',icon: ''}
          ,{name: '付款申请',url: '/DIYPages/general_entity/general_entity?entity=e_fukuanshenqing',icon: ''}
          ,{name: '付款申请（项目）',url: '/DIYPages/general_entity/general_entity?entity=e_fukuanshenqingxiangmu',icon: ''}
          ,{name: '备用金',url: '/DIYPages/general_entity/general_entity?entity=e_beiyongjin',icon: ''}
          ,{name: '备用金核销',url: '/DIYPages/general_entity/general_entity?entity=e_beiyongjinhexiao',icon: ''}
          ,{name: '备用金还款',url: '/DIYPages/general_entity/general_entity?entity=e_beiyongjinhuaikuan',icon: ''}
          ,{name: '项目业务费用登记',url: '/DIYPages/general_entity/general_entity?entity=e_xiangmuyewufeiyongdengji',icon: ''}
          ,{name: '收款审批流程',url: '/DIYPages/general_entity/general_entity?entity=e_shoukuanshenpiliucheng',icon: ''}
          ,{name: '收款单',url: '/DIYPages/general_entity/general_entity?entity=e_shoukuandan',icon: ''}
          ,{name: '付款单',url: '/DIYPages/general_entity/general_entity?entity=e_fukuandan',icon: ''}
          ,{name: '应收单',url: '/DIYPages/general_entity/general_entity?entity=e_yingshoudan',icon: ''}
          ,{name: '应收坏账',url: '/DIYPages/general_entity/general_entity?entity=e_yingshouhuaizhang',icon: ''}
          ,{name: '应收回款',url: '/DIYPages/general_entity/general_entity?entity=e_yingshouhuikuan',icon: ''}
          ,{name: '应付单',url: '/DIYPages/general_entity/general_entity?entity=e_yingfudan',icon: ''}
          ,{name: '应付实付',url: '/DIYPages/general_entity/general_entity?entity=e_yingfushifu',icon: ''}
          ,{name: '应付免付',url: '/DIYPages/general_entity/general_entity?entity=e_yingfumianfu',icon: ''}
          ,{name: '开票申请',url: '/DIYPages/general_entity/general_entity?entity=e_kaipiaoshenqing',icon: ''}
          ,{name: '薪酬批量支付',url: '/DIYPages/general_entity/general_entity?entity=e_xinchoupiliangzhifu',icon: ''}
          ,{name: '结算资料',url: '/DIYPages/general_entity/general_entity?entity=e_jiesuanziliao',icon: ''}

        ]
      }
      ,{
        title: '项目合同管理',
        nav:[
          {name: '合同帮助表',url: '/DIYPages/general_entity/general_entity?entity=e_bangzhubiaoshezhi',icon: ''}
          ,{name: '合同登记',url: '/DIYPages/general_entity/general_entity?entity=e_hetongdengji',icon: ''}
        ]
      }
      ,{
        title: '客户管理',
        nav:[
          {name: '客户信息修改',url: '/DIYPages/general_entity/general_entity?entity=e_kehuxinxixiugai',icon: ''}
        ]
      }
      ,{
        title: '订单管理',
        nav:[
          {name: '销售订单',url: '/DIYPages/general_entity/general_entity?entity=e_xiaoshoudingdan',icon: ''}
          ,{name: '采购订单',url: '/DIYPages/general_entity/general_entity?entity=e_OAcaigoudingdan',icon: ''}
        ]
      }
      ,{
        title: '行政法务管理',
        nav:[
          {name: '用印申请或资料借出',url: '/DIYPages/general_entity/general_entity?entity=e_yongyinshenqinghuoziliaojiechu',icon: ''}
          ,{name: '法务合同',url: '/DIYPages/general_entity/general_entity?entity=e_fawuhetong',icon: ''}
          ,{name: '子管理员权限申请',url: '/DIYPages/general_entity/general_entity?entity=e_ziguanliyuanquanxianshenqing',icon: ''}
        ]
      }
      ,{
        title: '司机注册审批',
        nav:[
          {name: '司机注册审批',url: '/DIYPages/general_entity/general_entity?entity=e_driver_registration',icon: ''}
        ]
      }
      ,{
        title: '其它',
        nav:[
          {name: '寄件申请',url: '/DIYPages/general_entity/general_entity?entity=e_jijianshenqing',icon: ''}
        ]
      }
    ]*/
  },
  onChange(e){
    this.setData({
      activeNames: e.detail,
    });
  },
  navTo(e){
    let {url} = e.currentTarget.dataset;
    wx.navigateTo({url})
  },
  //获取OA审批菜单
  getOAMenus(){
    return wx.$post(3,'Menu/query_menu',{"page":1,"limit":0,"oby":"sort"},false,true).then(res =>{
      return res?.filter(ite => ite.menuLabel === 'OA审批')
    })
      .then(menu =>{
        let child = menu[0]?.children,
          navs = [];
        child?.forEach(item =>{
          let obj = {
            title: item.menuLabel,
            nav: []
          };
          if (item.children?.length){
            obj.nav = this.formatNav(item.children)
          }
          if (item.menuURL){
            obj.nav = this.formatNav([item])
          }
          if (obj.nav.length)navs.push(obj);
        });
        this.setData({navs});
      })
  },
  formatNav(child){
    let arr =[],
      {funArr} = this.data;
    child.forEach(item =>{
      let {menuLabel,menuURL} = item,
      obj = {name: menuLabel};
      if (menuURL.indexOf('general_entity/show_entity.html') >=0){
        let entity = wx.$util.GetUrlString(menuURL,'entity');
        if (!funArr.includes(entity)) return;
        obj.url = `/DIYPages/general_entity/general_entity?entity=${entity}`;
        arr.push(obj);
      }
      if (item.children?.length){
        let cNav = this.formatNav(item.children);
        arr = [...arr,...cNav];
      }
    });
    return arr;
  },

  //获取后台权限
  getAuthority () {
    return new Promise(resolve => {
      let This = this;
      let userid = wx.getStorageSync('user_id'),
        post = {"user_id": userid,"function_id": '',"pare_id": ''};
      wx.$post(4,'QX/PRO_QX_list',post).then(res =>{
        if (res.errcode != 0 )return;
        let data = res.data?.row,
          arr = [];
        data?.forEach(item => {
          if(item.is_see){
            arr.push(item.function_id)
          }
        });
        this.data.funArr = arr;
        resolve()
      })
    })
  },
  test(){
    class Scheduler{
      constructor(){
        this.tasks = [];
        this.usingTask = [];
      }
      add(promiseCreator){
        return new Promise((resolve,reject) => {
         promiseCreator.resolve = resolve;
         if (this.usingTask.length < 2){
           this.usingRun(promiseCreator);
         } else {
           this.tasks.push(promiseCreator)
         }
        })
      }
      usingRun(promiseCreator){
        this.usingTask.push(promiseCreator);
        promiseCreator().then(() =>{
          promiseCreator.resolve();
          this.usingMove(promiseCreator)
          if (this.tasks.length >0){
            this.usingRun((this.tasks.shift()))
          }
        })
      }
      usingMove(promiseCreator){
        let index = this.usingTask.findIndex(promiseCreator);
        this.usingTask.splice(index,1)
      }
    }
    const timeout = (time) => new Promise(resolve => {
      setTimeout(resolve,time)
    })
    const scheduler = new Scheduler()

    const addTask = (time, order) =>{
      scheduler.add(() => timeout(time)).then(() => console.log(order))
    }
    addTask(400,4)
    addTask(200,2)
    addTask(300,3)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({mask: true})
    this.getAuthority().then(() => this.getOAMenus().then(() => wx.hideLoading()))

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
